package cn.shop.dys.adapter;

import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.lidroid.xutils.BitmapUtils;

import java.util.List;
import java.util.Map;

import cn.shop.dys.R;
import cn.shop.dys.activity.ProductListShowActivity;
import cn.shop.dys.bean.CategoryInfo;
import cn.shop.dys.constant.CommonConstant;
import cn.shop.dys.constant.Constant;
import cn.shop.lib_common.CommonUtil;


public class CategorySecondAdapter extends BaseAdapter {

    private Map<String, List<CategoryInfo>> thirdMap;
    List<CategoryInfo> list;
    private BitmapUtils bitmapUtils;

    public void setList(List<CategoryInfo> list) {
//            this.list.clear();

        this.list = list;
        notifyDataSetChanged();
        Log.i("test",list.toString());
    }

    public CategorySecondAdapter(List<CategoryInfo> list, BitmapUtils bitmapUtils,
                                 CategoryInfo headInfo, ImageView second_iv, TextView second_tv
            , Map<String, List<CategoryInfo>> thirdMap
    ) {
        this.list = list;
        this.bitmapUtils = bitmapUtils;
        this.thirdMap = thirdMap;
        Log.i("test",thirdMap.toString());
        bitmapUtils.display(second_iv, Constant.getUrl(headInfo.pic));
        second_tv.setText(headInfo.name);
    }

    @Override
    public int getCount() {

//        if(list!=null&&list.size()>0)
//            return list.size();
//        else
//            return 0;
return  1;
    }

    @Override
    public CategoryInfo getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        CategoryCommonViewHolder holder = new CategoryCommonViewHolder(parent.getContext(), convertView, R.layout.category_lv_second_item);
        CategoryInfo info = list.get(position);
        holder.getTextView(R.id.category_tv).setText(info.name);
        GridView gridView = holder.getGridView(R.id.listView);
       // GridView holderGridView = holder.getGridView(R.id.rcv);
        final List<CategoryInfo> third = thirdMap.get(info.id);
        gridView.setAdapter(new BaseAdapter() {
            @Override
            public int getCount() {
                return third.size();
            }

            @Override
            public CategoryInfo getItem(int position) {
                return third.get(position);
            }

            @Override
            public long getItemId(int position) {
                return position;
            }

            @Override
            public View getView(final int position, View convertView, ViewGroup parent) {
                CategoryCommonViewHolder viewHolder = new CategoryCommonViewHolder(parent.getContext(), convertView, R.id.sctoltools_tools);
                CategoryInfo categoryInfo = third.get(position);
                viewHolder.getTextView(R.id.gridview_tv).setText(categoryInfo.name);

                viewHolder.getView(R.id.gridview_lay, LinearLayout.class).setOnClickListener(new OnClickListener() {
                    @Override
                    public void onClick(View v) {

                        CommonUtil.showSingleToast(v.getContext(),"点击跳转");
                        CategoryInfo categoryInfo = third.get(position);
                        Intent intent = new Intent(v.getContext(), ProductListShowActivity.class);
                        intent.putExtra(Constant.KEY_NAME, categoryInfo.name);
                        intent.putExtra(Constant.KEY_ID, categoryInfo.id);
                        // TODO: 2016/4/18   传递数据
                        v.getContext().startActivity(intent);

                    }
                });
                bitmapUtils.display(viewHolder.getImageView(R.id.gridview_iv),Constant.getUrl(categoryInfo.pic));
                return viewHolder.convertView;
            }
        });

        gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

                CommonUtil.showSingleToast(parent.getContext(),"点击跳转");
                CategoryInfo categoryInfo = third.get(position);
                Intent intent = new Intent(parent.getContext(), ProductListShowActivity.class);
                intent.putExtra(Constant.KEY_NAME, categoryInfo.name);
                intent.putExtra(Constant.KEY_ID, categoryInfo.id);
                // TODO: 2016/4/18   传递数据
                parent.getContext().startActivity(intent);
            }
        });
        return holder.convertView;
    }
}